package com.hqq.web.page;

import com.alibaba.fastjson.JSONObject;
import com.hqq.domain.WoocUser;
import com.hqq.domain.WoocVideo;
import com.hqq.domain.WoocVideoRemark;
import com.hqq.enums.ResultEnum;
import com.hqq.exceptions.InvokeException;
import com.hqq.service.WoocVideoService;
import com.hqq.util.Response;
import com.hqq.vo.VideoVO;
import com.hqq.web.consts.WoocConsts;
import com.hqq.web.util.CookieUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * VideoController
 * Created by heqianqian on 2018/3/26.
 */
@Controller
@RequestMapping("/video")
public class VideoController {
    private static final Logger logger = LoggerFactory.getLogger(VideoController.class);

    @Autowired
    private WoocVideoService woocVideoService;

    /**
     * 跳转到列表详情页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(Long firstMenuId, Long secondMenuId, Model model, HttpServletRequest request) {
        logger.debug("===VideoController getVideoList===firstMenuId:{},secondMenuId:{}", firstMenuId, secondMenuId);
        try {
            model.addAttribute("firstMenuId", firstMenuId);
            model.addAttribute("secondMenuId", secondMenuId);
            String userInfo = CookieUtil.findCookie(request, WoocConsts.COOKIE_KEY);
            if (StringUtils.isNotBlank(userInfo)) {
                WoocUser user = JSONObject.parseObject(userInfo, WoocUser.class);
                if (null != user) {
                    model.addAttribute("name", user.getUserName());
                }
            }
        } catch (Exception e) {
            logger.error("===VideoController list===", e);
            return "error";
        }
        return "video/list";
    }

    /**
     * 跳转到列表详情页
     *
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Long videoId, Model model) {
        model.addAttribute("videoId", videoId);
        VideoVO video = woocVideoService.findVideo(videoId);
        model.addAttribute("videoUrl", video.getVideo().getVideoUrl());
        return "video/detail";
    }

    /*=============接口相关===========*/

    /**
     * 获取视频列表信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_video_list.ajax")
    public Response getVideoList(Long typeId, Long sortType, Long rankId, Integer pageNum) {
        logger.debug("===VideoController getVideoList===typeId:{},sortType:{},rankId:{},pageNum:{}", typeId, sortType, rankId, pageNum);
        Response response = null;
        try {
            List<WoocVideo> videoList = woocVideoService.getVideoList(typeId, sortType, rankId, pageNum);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), videoList);
        } catch (Exception e) {
            logger.error("===VideoController getVideoList===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 获取视频详细信息
     *
     * @param videoId
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_video_info.ajax")
    public Response getVideoInfo(Long videoId) {
        logger.debug("===VideoPlayController getVideoInfo===videoId:{}", videoId);
        Response response = null;
        try {
            //查询视频信息 封装VO对象 封装视频信息和评论信息
            VideoVO videoVO = woocVideoService.findVideo(videoId);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), videoVO);
        } catch (Exception e) {
            logger.error("===VideoPlayController getVideoInfo===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 发布视频评论
     *
     * @param videoId
     * @param remarkContent
     * @return
     */
    @ResponseBody
    @RequestMapping("/publish_remark.ajax")
    public Response publishRemark(Long videoId, String remarkContent, HttpServletRequest request) {
        logger.debug("===VideoPlayController publishRemark===videoId:{},remarkContent:{}", videoId, remarkContent);
        Response response = null;
        try {
            String userInfo = CookieUtil.findCookie(request, WoocConsts.COOKIE_KEY);
            if (StringUtils.isNotBlank(userInfo)) {
                WoocUser user = JSONObject.parseObject(userInfo, WoocUser.class);
                if (null != user) {
                    WoocVideoRemark remark = woocVideoService.publishRemark(user.getUserId(), videoId, remarkContent);
                    response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), remark);
                    return response;
                }
            }
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        } catch (Exception e) {
            logger.error("===VideoPlayController publishRemark===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 加载视频评论
     *
     * @param videoId
     * @return
     */
    @ResponseBody
    @RequestMapping("/list_remark.ajax")
    public Response listRemark(Long videoId) {
        logger.debug("===VideoPlayController listRemark===videoId:{},remarkContent:{}", videoId);
        Response response = null;
        try {
            VideoVO video = woocVideoService.findVideo(videoId);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription(), video.getRemarkList());
        } catch (Exception e) {
            logger.error("===VideoPlayController publishRemark===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }


    /**
     * 上传视频
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/upload_video.ajax")
    public Response uploadVideo(WoocVideo woocVideo, MultipartHttpServletRequest request) {
        logger.debug("===VideoPlayController uploadVideo===woocVideo:{}", woocVideo);
        Response response = null;
        try {
            MultipartFile videoFile = request.getFile("video");
            MultipartFile videoProfileFile = request.getFile("profile");
            logger.debug("===VideoPlayController uploadVideo===file:{},name:{}", videoFile.getSize(), videoFile.getOriginalFilename());
            logger.debug("===VideoPlayController uploadVideo===videoProfileFile:{},name:{}", videoProfileFile.getSize(), videoProfileFile.getOriginalFilename());
            String userInfo = CookieUtil.findCookie(request, WoocConsts.COOKIE_KEY);
            if (StringUtils.isNotBlank(userInfo)) {
                WoocUser user = JSONObject.parseObject(userInfo, WoocUser.class);
                if (null != user) {
                    woocVideo.setUserId(user.getUserId());
                    WoocVideo video = woocVideoService.insertVideo(woocVideo, videoFile,videoProfileFile);
                    response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), video);
                    return response;
                }
            }
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        } catch (Exception e) {
            logger.error("===VideoPlayController uploadVideo===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }


    /**
     * 删除视频
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete_video.ajax")
    public Response deleteVideo(Long videoId) {
        logger.debug("===VideoPlayController deleteVideo===videoId:{}", videoId);
        Response response = null;
        try {
            woocVideoService.deleteVideo(videoId);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription());
        } catch (Exception e) {
            logger.error("===VideoPlayController deleteVideo===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }


}
